#include "node_item.h"

NodeItem::NodeItem()
{

}

NodeItem::NodeItem(QString title):QGraphicsItem()
{
    this->title = title;
    title_color= new QColor("#aaaaaa");
    text = new QGraphicsTextItem(this->title,this);  // 注意设置父级为自身
    text->setDefaultTextColor(*this->title_color);  //文本颜色
    text->setPos(4,2);
    //text->setTextWidth(-1);

    // 嵌入的Content
    this->proxyContent = new QGraphicsProxyWidget(this);
    this->content = new NodeContent();
    this->content->setGeometry(edge_size, title_height+0.5*edge_size,
                               width - 2*edge_size,height - 2*edge_size - title_height);
    //将nodeContent嵌入proxyContent
    this->proxyContent->setWidget(this->content);

    // opengl 嵌入 proxyContent
//    CoreFunctionWidget *gl = new CoreFunctionWidget();
//    this->proxyContent->setWidget(gl);



    this->setFlag(QGraphicsItem::ItemIsSelectable);
    this->setFlag(QGraphicsItem::ItemIsMovable);


}

NodeItem::NodeItem(const NodeItem &nodeItem)
{
    Q_UNUSED(nodeItem);
}

void NodeItem::operator=(const NodeItem &nodeItem)
{
    Q_UNUSED(nodeItem);
}


NodeItem::~NodeItem()
{
    delete title_color;
    delete text;
    delete proxyContent;
    delete content;
    title_color = NULL;
    text = NULL;
    proxyContent = NULL;
    content = NULL;
}

void NodeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);
    QPainterPath *path_title = new QPainterPath();
    path_title->setFillRule(Qt::WindingFill);
    path_title->addRoundedRect(0,0,width,title_height,edge_size,edge_size);//创建椭圆
    path_title->addRect(0,title_height-edge_size,edge_size,edge_size);//给椭圆左侧增加Rect遮住倒角
    path_title->addRect(width-edge_size,title_height-edge_size,edge_size,edge_size);//给椭圆右侧增加Rect遮住倒角
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor("#FF313131")); //E3212121
    painter->drawPath(path_title->simplified());

    QPainterPath *path_data = new QPainterPath();
    path_data->setFillRule(Qt::WindingFill);
    path_data->addRoundedRect(0,title_height,width,height-title_height,edge_size,edge_size);//创建椭圆
    path_data->addRect(0,title_height,edge_size,edge_size);//给椭圆左侧增加Rect遮住倒角
    path_data->addRect(width-edge_size,title_height,edge_size,edge_size);//给椭圆右侧增加Rect遮住倒角
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor("#E3212121"));
    painter->drawPath(path_data->simplified());



    QPainterPath *path_outline =new QPainterPath();
    path_outline->addRoundedRect(0,0,width,height,edge_size,edge_size);
    if(!this->isSelected()){
        painter->setPen(QPen(QColor("#aaaaaa")));
    }
    else{
        painter->setPen(QPen(QColor("#FFFFA637")));
    }
    painter->setBrush(Qt::NoBrush);
    painter->drawPath(path_outline->simplified());
}

QRectF NodeItem::boundingRect() const
{
    return QRectF(
        0,
        0,
        width,height
//        2 * edge_size * width,
//        2 * edge_size * height
    );
    //.normalized()?
    // .normalized() maybe diff with no_nor,but do not known the effect
}

void NodeItem::SetTitle(QString title)
{
    this->title = title;
    this->text->setPlainText(title);
}


//QDataStream &operator<<(QDataStream &output,const NodeItem &item){

//    return output;
//}
//QDataStream &operator>>(QDataStream &input,NodeItem &item){
//    while(!input.atEnd()){
//        input >>item.title;
//        input >>*item.title_color;
//        input >>*item.text;
//        input >>*item.proxyContent;
//        input >>*item.content;

//    }
//    return input;
//}

